package com.ruoyi.web.controller.evaluation;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.system.domain.evaluation.Location;
import com.ruoyi.system.service.evaluationService.LocationService;
import net.sf.json.JSONObject;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.TreeMap;

@Controller
@RequestMapping("/evaluation/location")
public class LocationController extends BaseController {
    @Autowired
    private LocationService locationService;
    String prefix = "evaluation";

    /**
     * 跳转地址页面
     * @return
     */
    @RequiresPermissions("evaluation:location:view")
    @GetMapping("/location")
    public String location(){
        return "evaluation/location";
    }

    /**
     * 返回地址数据
     * @return
     */
    @RequiresPermissions("evaluation:location:view")
    @PostMapping("/listLocation")
    @ResponseBody
    public TableDataInfo list(){
        startPage();
        List<Location> locations = locationService.selectLocation();
        return getDataTable(locations);
    }

    /**
     * 返回地址数据
     * @return
     */
    @RequiresPermissions("evaluation:location:view")
    @PostMapping("/locationList")
    @ResponseBody
    public String locationList(){
        List<Location> locations = locationService.selectLocation();
        Map out = new TreeMap();
        out.put("locations",locations);
        JSONObject jsonObject = JSONObject.fromObject(out);
        return jsonObject.toString();
    }
    @GetMapping("/add")
    public String add(){
        return prefix+"/add";
    }
    /**
     *添加地址
     */
    @RequiresPermissions("evaluation:location:add")
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addLocation(Location location){
        System.out.println(location);
        int num = locationService.insertLocation(location);
        if (num>0){
    return success();
        }else{
            return error();
        }
    }
    /**
     *删除地址
     */
    @RequiresPermissions("evaluation:location:remove")
   @PostMapping("remove/{id}")
   @ResponseBody
    public AjaxResult removeLocation(@PathVariable("id")int  id){
        int num = locationService.deleteLocationById(id);
        if (num>0){
            return success();
        }else{
            return error();
        }
    }
    /**
     *删除地址
     */
    @RequiresPermissions("evaluation:location:remove")
    @PostMapping("remove2")
    @ResponseBody
    public AjaxResult removeLocation2(String ids){
        int num = locationService.deleteLocation(ids);
        if (num>0){
            return success();
        }else{
            return error();
        }
    }
}
